package com.rtwo.beauty;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.*;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.orhanobut.logger.Logger;

public class MainActivity extends Activity implements ViewSwitcher.ViewFactory {
    private ImageSwitcher imageSwitcher;
    private Gallery gallery;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setupListener();

    }

    private void init(){
        imageSwitcher = (ImageSwitcher)findViewById(R.id.imageswitch);
        gallery = (Gallery)findViewById(R.id.gallery);
        imageSwitcher.setFactory(this);
        gallery.setAdapter(new MyAdapter(this));

    }

    @Override
    public View makeView() {
        ImageView i = new ImageView(this);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight() - 120));
        return i;
    }

    private void setupListener(){
        gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                int drawableId = 0;
                try {
                    drawableId = R.drawable.class.getDeclaredField("g" + (position + 1) + "_up").getInt(this);
                    MainActivity.this.position = position;
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
                imageSwitcher.setImageResource(drawableId);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        imageSwitcher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("position", position);
                intent.setClass(MainActivity.this, RemoveActivity.class);
                MainActivity.this.startActivity(intent);
            }
        });
    }

    @Override
    public void onBackPressed(){
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
    }

    class MyAdapter extends BaseAdapter{

        private Context context;

        public MyAdapter(Context contenxt){
            this.context = contenxt;
        }

        @Override
        public int getCount() {
            return 21;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(context);
            int drawableId;
            try {
                drawableId = R.drawable.class.getDeclaredField("g"+(position+1)+"_icon").getInt(this);
                imageView.setLayoutParams(new Gallery.LayoutParams(120,120));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setImageResource(drawableId);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            return imageView;
        }
    }
}






















